/**
  ******************************************************************************
  * File Name          : weight.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "route.h"

static Route_PayloadType lx_cycle = {0};

void Route_lx_485_burst(Route_PayloadType *rtpl)
{
  uint8_t i;
  
  for(i=0; i<3; i++)
  {
    xQueueReset(Route_485RxLink_Qhd);
    
    RS485_LinkSend(rtpl);
    
    if(xQueueReceive(Route_485RxLink_Qhd, &Route_485_rx_link_bck, 100) == pdTRUE)
    {
      if((Route_485_rx_link_bck.dev_addr == rtpl->dev_addr) && \
         (Route_PayloadCRC(&Route_485_rx_link_bck, ROUTE_PLCRC_CHECK) == 0))
      {
        if(Route_485_rx_link_bck.trans_dir == ROUTE_TRSDIR_SUBDEV_TO_NET)
        {
          if(Route_EvewToNet_Qhd != NULL)
          {
            xQueueSend(Route_EvewToNet_Qhd, &Route_485_rx_link_bck, 0);
          }
          return;
        }
        else if(Route_485_rx_link_bck.trans_dir == ROUTE_TRSDIR_SUBDEV_TO_LOCAL)
        {
          return;
        }
      }
    }
  }
}

void Route_lx_485_cycle(uint8_t addr)
{
  lx_cycle.dev_addr  = idx2add(addr);
  lx_cycle.dev_type  = RT_DV_LX;
  lx_cycle.trans_dir = ROUTE_TRSDIR_LOCAL_TO_SUBDEV;
  lx_cycle.pl_type   = ROUTE_PLTYP_LX_CYCLE;
  lx_cycle.pl_len    = 0;
  
  xQueueReset(Route_485RxLink_Qhd);
  
	RS485_LinkSend(&lx_cycle);
	
	if(xQueueReceive(Route_485RxLink_Qhd, &Route_485_rx_link_bck, 100) == pdTRUE)
	{
    if((Route_485_rx_link_bck.dev_addr == lx_cycle.dev_addr) && (Route_PayloadCRC(&Route_485_rx_link_bck, ROUTE_PLCRC_CHECK) == 0))
    {
      if(Route_485_rx_link_bck.trans_dir == ROUTE_TRSDIR_SUBDEV_TO_NET)
      {
        if(Route_EvewToNet_Qhd != NULL)
        {
          xQueueSend(Route_EvewToNet_Qhd, &Route_485_rx_link_bck, 0);
        }
        return;
      }
      else if(Route_485_rx_link_bck.trans_dir == ROUTE_TRSDIR_SUBDEV_TO_LOCAL)
      {
        return;
      }
    }
	}
}



